/**
 * 工具包
 *
 * @author liuqi
 * @date 2024/12/20 20:50
 **/
import request from "@/util/request";
import setter from "@/setting/setter";
import Vue from "vue";
import { Loading } from "element-ui";
export default {

    /**
     * JSON对象合并, <code>source</code>合并至<code>target</code>
     *
     * @param target
     * @param source
     * @param exclude  排除key
     * @returns {*}
     */
    merge(target, source, exclude) {
        for (let key in source) {
            let o = source[key];
            let t = target[key];
            if (this.isObject(o)) {
                this.merge(this.isEmpty(t) ? {} : t, o);
            } else {
                if (exclude == key) {
                    continue;
                }
                target[key] = o;
            }
        }
        return target;
    },

    /**
     * 拷贝对象
     */
    copy(source) {
        let target = {};
        return this.merge(target, source);
    },

    /**
     * 是否为对象
     *
     */
    isObject(object) {
        if (this.isEmpty(object)) return false;
        return Object.prototype.toString.call(object) == "[object Object]";
    },

    /**
     * 是否是函数
     *
     */
    isFunction(func) {
        return this.isNotEmpty(func) && typeof func == "function";
    },

    /**
     * 是否为空
     *
     */
    isEmpty(object) {
        if (object == undefined || object == null || object.toString().length == 0) {
            return true;
        }
        if (Array.isArray(object)) {
            return object.length <= 0;
        }
        return false;
    },

    /**
     * 非空
     *
     */
    isNotEmpty(object) {
        return !this.isEmpty(object);
    },

    /***
     * 获取URL的参数
     *
     * @param name
     * @returns {string|null}
     */
    getUrlParams(name) {
        if (name == null || name === 'undefined') {
            return null;
        }
        let searchStr = decodeURIComponent(window.location.href).replace("?", "&");
        let infoIndex = searchStr.indexOf(name + '=');
        if (infoIndex === -1) {
            return null;
        }
        let searchInfo = searchStr.substring(infoIndex + name.length + 1);
        let tagIndex = searchInfo.indexOf("&");
        if (tagIndex !== -1) {
            searchInfo = searchInfo.substring(0, tagIndex);
        }
        return searchInfo;
    },

    requestURL(url) {
        let baseURL = setter.server.request.baseURL;
        if (url.startsWith("/")) {
            return baseURL + url;
        } else {
            return baseURL + "/" + url;
        }
    },

    /**
     * Http URL入参序列化
     *
     * @param data json对象
     */
    httpQuery(data) {
        if (this.isEmpty(data)) return "";
        let keys = Object.keys(data);
        if (keys.length == 0) return "";
        let query = [encodeURIComponent(keys[0]), "=", encodeURIComponent(data[keys[0]])];
        for (let i = 1; i < keys.length; i++) {
            query.push("&");
            query.push(encodeURIComponent(keys[i]), "=", encodeURIComponent(data[keys[i]]));
        }
        return query.join("");
    },

    timestampToDate(timestamp) {
        let date = new Date(timestamp);
        let year = date.getFullYear();
        let month = dateFormat(date.getMonth() + 1); // 月份从0开始，需要加1
        let day = dateFormat(date.getDate());
        let hours = dateFormat(date.getHours());
        let minutes = dateFormat(date.getMinutes());
        let seconds = dateFormat(date.getSeconds());
        return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
    },

    dateFormatYM(date) {
        let year = date.getFullYear();
        let month = dateFormat(date.getMonth() + 1); // 月份从0开始，需要加1
        return `${year}-${month}`;
    },

    dateFormatYMD(date) {
        let year = date.getFullYear();
        let month = dateFormat(date.getMonth() + 1); // 月份从0开始，需要加1
        let day = dateFormat(date.getDate());
        return `${year}-${month}-${day}`;
    },

    get(url, params) {
        return request.get(url, params);
    },

    delete(url, params) {
        return request.delete(url, params);
    },

    post(url, params) {
        return request.post(url, params);
    },

    postForm(url, params) {
        return request.postForm(url, params);
    },

    loading(text) {
        const loading = Vue.prototype.$loading({
            spinner: "el-icon-loading",
            target: ".main-container",
            text: text || "加载中. . .",
            background: "rgba(0, 0, 0, 0.4)"
        });
        return loading;
    },

    closeAllLoading() {
        document.querySelectorAll(".el-loading-mask").forEach(el => {
            el.parentNode?.removeChild(el)
        })
    }

}

function dateFormat(t) {
    if (t.toString().length == 1) {
        return "0" + t;
    }
    return t;
}
